package com.sinosoft.ptas.service.impl;

import java.util.List;
import java.util.UUID;

import javax.validation.Valid;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.ptas.dao.IndexMapper;
import com.sinosoft.ptas.dao.ReportEntityMapper;
import com.sinosoft.ptas.dao.TargetsOfImplPhaseMapper;
import com.sinosoft.ptas.entity.IndexEntity;
import com.sinosoft.ptas.entity.ReportEntity;
import com.sinosoft.ptas.entity.TargetsOfImplPhaseEntity;
import com.sinosoft.ptas.service.IndexService;
import com.sinosoft.ptas.system.commom.ExceptionConstant;
/**
 * 指标管理接口实现
 * @author lianWuJun
 * @time   2017年2月18日 下午5:01:33
 */
@Validated
public class IndexServiceImpl implements IndexService {
	
	IndexMapper indexMapper;
	TargetsOfImplPhaseMapper targetsOfImplPhaseMapper;
	ReportEntityMapper reportMapper;
	
	
	public void setReportMapper(ReportEntityMapper reportMapper) {
		this.reportMapper = reportMapper;
	}

	public void setTargetsOfImplPhaseMapper(
			TargetsOfImplPhaseMapper targetsOfImplPhaseMapper) {
		this.targetsOfImplPhaseMapper = targetsOfImplPhaseMapper;
	}

	public void setIndexMapper(IndexMapper indexMapper) {
		this.indexMapper = indexMapper;
	}

	/**
	 * 创建指标
	 * @param indexEntity
	 * @return 实体编号
	 */
	public String createIndex(@Valid IndexEntity indexEntity) throws BusinessException{		
		if(indexEntity==null){
			throw new BusinessException(ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE);
		}
		String id=UUID.randomUUID().toString();
		indexEntity.setId(id);
		indexMapper.createIndex(indexEntity);
		return id;
	}

	/**
	 * 根据指标编号查询指标
	 * @param id
	 * @return 指标类型实体
	 */

	public IndexEntity retrieveIndex(String id) {
		if(StringUtil.isBlank(id)){
			throw new  BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		}
		return indexMapper.retrieveIndex(id);
	}

	/**
	 * 根据指标类的属性查询指标
	 * 如果没有参数为空 则查询全部
	 * @param indexEntity
	 * @return 指标类型集合
	 */
	public List<IndexEntity> retrieveIndexs(IndexEntity indexEntity) {
		return indexMapper.retrieveIndexs(indexEntity);
	}

	/**
	 * 更新指标
	 * @param indexEntity
	 */
	public void updateIndex(@Valid IndexEntity indexEntity) throws BusinessException{
		if(indexEntity==null){
			throw new BusinessException(ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE);
		} 
		indexMapper.updateIndex(indexEntity);
	}

	/**
	 * 删除指标
	 * @param id
	 */
	public void deleteIndex(String id) throws BusinessException{
		if(StringUtil.isBlank(id)){
			throw new  BusinessException(ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE);
		} 
		indexMapper.deleteIndex(id);
	}
	
	
	/**
	 * 批量更新指标值
	 * @param indexs
	 */
	public void updateIndexs(List<IndexEntity> indexs){
		
		indexMapper.updateIndexsForGCS(indexs);//规財司设置默认参考值
		indexMapper.updateIndexs(indexs);//其他司设置默认参考值
		
	}
	
	/**
	 * 批量更新指标值
	 * @param indexs
	 */
	@Transactional(rollbackFor=Exception.class)
	public void updateIndexs(List<IndexEntity> indexs,String statu) throws BusinessException{
		TargetsOfImplPhaseEntity targetsO=new TargetsOfImplPhaseEntity();
		String getTargetsOfImplPhase=indexMapper.retrieveGcsIndexById(indexs.get(0).getId()).getTargetsOfImplPhase();//查询出值表的实施期目标id
		TargetsOfImplPhaseEntity targets = targetsOfImplPhaseMapper.retrieveGcsTargetsOfImplPhaseById(getTargetsOfImplPhase);
		String reportId=targets.getReportId();
		if(statu.equals("0")){//如果传入的状态是保存状态
			if(targets.getReferValueIsFinsh()!=null&&targets.getReferValueIsFinsh().equals("0")){//如果是保存状态
				targets.setReferValueIsFinsh(statu);
				targetsOfImplPhaseMapper.updateTargetsOfImplPhaseForGcs(targets);
				indexMapper.updateIndexsForGCS(indexs);//规財司设置默认参考值
				indexMapper.updateIndexs(indexs);//其他司设置默认参考值
				
			}else if(targets.getReferValueIsFinsh()!=null&&targets.getReferValueIsFinsh().equals("1")){//如果是提交状态
				throw new BusinessException("001","指标参考值设置已经提交,不能进行修改");
			}else{
				targetsO.setId(getTargetsOfImplPhase);//给实施期目标设置标志
				targetsO.setReferValueIsFinsh(statu);
				targetsOfImplPhaseMapper.updateTargetsOfImplPhaseForGcs(targetsO);
				indexMapper.updateIndexsForGCS(indexs);//规財司设置默认参考值
				indexMapper.updateIndexs(indexs);//其他司设置默认参考值
			}
		}else if(statu.equals("1")){//如果传入的状态是提交状态
			if(targets.getReferValueIsFinsh()!=null&&targets.getReferValueIsFinsh().equals("1")){
				throw new BusinessException("001","指标参考值设置已经提交,不能进行修改");
			}else{
				ReportEntity report=new ReportEntity();
				targetsO.setId(getTargetsOfImplPhase);//给实施期目标设置标志
				targetsO.setReferValueIsFinsh(statu);
				targetsOfImplPhaseMapper.updateTargetsOfImplPhaseForGcs(targetsO);
				indexMapper.updateIndexsForGCS(indexs);//规財司设置默认参考值
				indexMapper.updateIndexs(indexs);//其他司设置默认参考值
				report.setReferValueIsFinsh(reportMapper.retrieveGcsReportById(reportId).getReferValueIsFinsh()+1);
				report.setId(reportId);
				reportMapper.updateReportGcs(report);
			}
		}
		
		
		
		
		
		
		
		
		
		
		/*
		if(targets.getReferValueIsFinsh()!=null&&targets.getReferValueIsFinsh().equals("0")){//如果是保存状态
			targets.setReferValueIsFinsh(statu);
			targetsOfImplPhaseMapper.updateTargetsOfImplPhaseForGcs(targets);
			indexMapper.updateIndexsForGCS(indexs);//规財司设置默认参考值
			indexMapper.updateIndexs(indexs);//其他司设置默认参考值
			
		}else if(targets.getReferValueIsFinsh()!=null&&targets.getReferValueIsFinsh().equals("1")){//如果是提交状态
			throw new BusinessException("001","指标参考值设置已经提交,不能进行修改");
		}else{//如果这个实施期目标没有referValueIsFinsh为空的话
			if(statu.equals("0")){
				targetsO.setId(getTargetsOfImplPhase);//给实施期目标设置标志
				targetsO.setReferValueIsFinsh(statu);
				targetsOfImplPhaseMapper.updateTargetsOfImplPhaseForGcs(targetsO);
				indexMapper.updateIndexsForGCS(indexs);//规財司设置默认参考值
				indexMapper.updateIndexs(indexs);//其他司设置默认参考值
			}else if(statu.equals("1")){
				ReportEntity report=new ReportEntity();
				targetsO.setId(getTargetsOfImplPhase);//给实施期目标设置标志
				targetsO.setReferValueIsFinsh(statu);
				targetsOfImplPhaseMapper.updateTargetsOfImplPhaseForGcs(targetsO);
				indexMapper.updateIndexsForGCS(indexs);//规財司设置默认参考值
				indexMapper.updateIndexs(indexs);//其他司设置默认参考值
				report.setReferValueIsFinsh(reportMapper.retrieveGcsReportById(reportId).getReferValueIsFinsh()+1);
				report.setId(reportId);
				reportMapper.updateReportGcs(report);
			}
		}*/
	}

}
